package com.springboot.study.tests.supplier;

import java.util.Date;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 * @Author: guodong
 * @Date: 2020/11/26 10:52
 * @Version: 1.0
 * @Description:
 */
public class ConsumerTest {

    public static void main(String[] args) {
        Consumer<Integer> consumer = (x) -> {
            int num = x * 2;
            System.out.println(num);
        };

        Consumer<Integer> consumer1 = (x) -> {
            int num = x * 3;
            System.out.println(num);
        };

//        consumer.andThen(consumer1).accept(10);
        consumer.accept(100);

        cal();
    }



    public static void cal(){
        Cat cat = Cat.creat(Cat::new);
        cat.setBirthDay(new Date());
        cat.setId(10000L);
        cat.setName("I am a cat");

        Consumer<Cat> catConsumer = (x) ->{
            // 处理业务逻辑
            System.out.println(x);
        };

        // 该函数式接口的唯一的抽象方法,接收一个参数,没有返回值.
        // 传入x 这个对象
        catConsumer.accept(cat);



        test_Consumer();
    }


    public static void test_Consumer() {
        //① 使用consumer接口实现方法
        Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };

        Consumer<Cat> catConsumer = new Consumer<Cat>() {
            @Override
            public void accept(Cat cat) {
                System.out.println(cat);
                System.out.println(cat.getId());
            }
        };

        Stream<String> stream = Stream.of("aaa", "bbb", "ddd", "ccc", "fff");
        stream.forEach(consumer);

        System.out.println("********************");

        //② 使用lambda表达式，forEach方法需要的就是一个Consumer接口
        stream = Stream.of("aaa", "bbb", "ddd", "ccc", "fff");
        //lambda表达式返回的就是一个Consumer接口
        Consumer<String> consumer1 = (s) -> System.out.println(s);
        stream.forEach(consumer1);


        //更直接的方式
        //stream.forEach((s) -> System.out.println(s));
        System.out.println("********************");

        //③ 使用方法引用，方法引用也是一个consumer
        stream = Stream.of("aaa", "bbb", "ddd", "ccc", "fff");
        Consumer consumer2 = System.out::println;
        stream.forEach(consumer);
        //更直接的方式
        //stream.forEach(System.out::println);
    }



}
